package com.asa.jade;

import com.asa.util.Time;
import org.lwjgl.Version;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;

import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;

public class Window {
    private int width,height;
    private String title;
    private long glfwWindow;
    private static Window window = null;

    private static Scene currentScene = null;

    public static float r = 0;
    public static float g = 0;
    public static float b = 0;



    private Window(){
        this.width = 1920;
        this.height = 1080;
        this.title = "Mario";
    }

    public static void changeScne(int newScene){
        switch (newScene){
            case 0:
                currentScene = new LevelEditorScene();
                currentScene.init();
                break;
            case 1:
                currentScene = new LevelScene();
                currentScene.init();

                break;
            default:
                assert false:"Unknown scene '"+ newScene +"'";
                break;
        }
    }


    public static Window get(){

        if (window ==null){
            window = new Window();
        }
        return window;

    }


    public void run(){
        System.out.println("Hello LWJGL " + Version.getVersion()+"!");
        init();

        r = 1;
        g = 1;
        b = 1;



        loop();


    }


    public void init(){
        //错误回调
        GLFWErrorCallback.createPrint(System.err).set();


        if (!glfwInit()){
            throw new IllegalStateException("Unable to initialize GLFW!");
        }
        //配置设置
        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_VISIBLE,GLFW_FALSE);
        glfwWindowHint(GLFW_RESIZABLE,GLFW_TRUE);
        glfwWindowHint(GLFW_MAXIMIZED,GLFW_TRUE);

        //创建窗口
        glfwWindow = glfwCreateWindow(this.width,this.height,this.title,NULL,NULL);

        if (glfwWindow == NULL){
            throw new IllegalStateException("Failed to create the GLFW Window!");
        }

        glfwSetCursorPosCallback(glfwWindow,MouseListener::mousePosCallback);
        glfwSetMouseButtonCallback(glfwWindow,MouseListener::mouseButtonCallback);
        glfwSetScrollCallback(glfwWindow,MouseListener::mouseScrollCallback);

        glfwSetKeyCallback(glfwWindow,KeyListener::keyCallback);

        //设置OpenGL上下文为当前
        glfwMakeContextCurrent(glfwWindow);
        //垂直同步
        glfwSwapInterval(1);

        //设置窗口可见
        glfwShowWindow(glfwWindow);

        //将LWJGL和OpenGL绑定，这一步很关键
        GL.createCapabilities();

        //释放内存空间
//        glfwFreeCallbacks(glfwWindow);
//        glfwDestroyWindow(glfwWindow);
//        glfwTerminate();
//        glfwSetErrorCallback(null).free();
        Window.changeScne(0);
    }


    public void loop(){
        float beginTime = Time.getTim();
        float endTime = Time.getTim();
        float dt = -1.0f;
        while (!glfwWindowShouldClose(glfwWindow)){
            //监听按键
            glfwPollEvents();

            glClearColor(r,g,b,1.0f);

            glClear(GL_COLOR_BUFFER_BIT);


            if (dt>=0){
                currentScene.update(dt);
            }



            //缓存区
            glfwSwapBuffers(glfwWindow);

            endTime = Time.getTim();
            dt = endTime - beginTime;
            beginTime = endTime;

        }



    }






}
